#include<iostream>
#include<algorithm>
#include<vector>
#include<string>
#include<map>

using namespace std;

int q, n;
string start;
int k, t;

struct item
{
	int st, end, k;
	
	bool operator <(const item& i)
	{
		return st != i.st ? st < i.st : end < i.end;
	}
};

int main()
{
	cin >> q;
	while (q--)
	{
		cin >> n;
		map<int, int> time_point;
		for (int i = 0; i < n; i++)
		{
			cin >> start >> k >> t;
			int st = atoi(start.substr(0, 2).c_str()) * 60 + atoi(start.substr(3, 2).c_str());
			int end = st + t;
			time_point[st] += k;
			time_point[end] -= k;
		} 

		int res = 0,sum = 0;
		for (auto it : time_point)
		{
			sum += it.second;
			res = max(res, sum);
		}

		cout << res << endl;
	}
	return 0;
}